<?php

namespace App\Model\Examples;

use PhalApi\Model\NotORMModel as NotORM;

/**
 *景点详情model
 */
class SpotView extends NotORM
{

    protected function getTableName($id)
    {
        return 'spot_view';
    }

    public function getListItems($page, $perpage, $city_id)
    {
//        $total = $this->getORM();
//        if (!empty($city_id)) {
//            $total->where('city_id', $city_id);
//        }
//        $total->where('status_id', '1')
//            ->order('createtime DESC')
//            ->limit(($page - 1) * $perpage, $perpage);
//        return $total;
        //            ->fetchAll();
        $sql1 = 'SELECT * FROM spot_type,spot_view WHERE spot_view.spot_type = spot_type.id ';
        if(!empty($city_id)){
            $sql1 .= 'and spot_view.city_id !=:city_id ';
        }
         $sql1 .= ' and spot_view.status_id =1 order by createtime desc limit :start,:size';
        if(!empty($city_id)) {
            $params = array(':city_id' => $city_id
            , ':start' => ($page - 1) * $perpage, ':size' => $perpage);
        }else{
            $params = array(':start' => ($page - 1) * $perpage, ':size' => $perpage);
        }
        $rs = $this->getORM()->queryAll($sql1, $params);
        return $rs;
    }

    public function getListTotal($word, $sort_type_id, $city_id)
    {
        $total = $this->getORM();
        if (!empty($word)) {
            $total = $total->where('title LIKE ?', '%' . $word . '%');
        }
        if (!empty($sort_type_id)) {
            $total = $total->where('spot_type', $sort_type_id);
        }
        if (!empty($city_id)) {
            $total = $total->where('city_id', $city_id);
        }
        $total = $total->where('status_id', '1')->count('id');

        return intval($total);
    }

    public function getSpotViewByWord($word, $sort_type_id, $page, $perpage)
    {
        $total = $this->getORM();
        if (!empty($word)) {
            $total = $total->where('title LIKE ?', '%' . $word . '%');
        }
        if (!empty($sort_type_id)) {
            $total = $total->where('spot_type', $sort_type_id);
        }
        $total = $total->limit(($page - 1) * $perpage, $perpage);
        return $total;
    }

}
